import 'package:flutter/material.dart';
import 'package:ducafe_ui_core/ducafe_ui_core.dart';

/// 可复用的自定义输入框组件
class CustomTextField extends StatelessWidget {
  /// 提示文字
  final String? hintText;

  /// 输入框控制器
  final TextEditingController? controller;

  /// 输入类型
  final TextInputType? keyboardType;

  /// 是否为密码输入框
  final bool obscureText;

  /// 输入框宽度，默认为313.21
  final double? width;

  /// 输入框高度，默认为46.34
  final double? height;

  /// 输入完成回调
  final ValueChanged<String>? onChanged;

  /// 提交回调
  final ValueChanged<String>? onSubmitted;

  /// 是否启用输入框
  final bool enabled;

  /// 最大行数
  final int? maxLines;

  /// 前缀图标
  final Widget? prefixIcon;

  /// 后缀图标
  final Widget? suffixIcon;

  /// 最大字符长度
  final int? maxLength;

  const CustomTextField({
    super.key,
    this.hintText,
    this.controller,
    this.keyboardType,
    this.obscureText = false,
    this.width,
    this.height,
    this.onChanged,
    this.onSubmitted,
    this.enabled = true,
    this.maxLines = 1,
    this.prefixIcon,
    this.suffixIcon,
    this.maxLength,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      width: width ?? 313.21.w,
      height: height ?? 46.34.h,
      decoration: ShapeDecoration(
        color: Colors.white.withValues(alpha: 0.39),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10.r),
        ),
      ),
      child: TextField(
        controller: controller,
        keyboardType: keyboardType,
        obscureText: obscureText,
        onChanged: onChanged,
        onSubmitted: onSubmitted,
        textInputAction: TextInputAction.done,
        enabled: enabled,
        maxLines: maxLines,
        maxLength: maxLength,
        style: TextStyle(
          color: Colors.black,
          fontSize: 14.sp,
          fontFamily: 'Manrope',
          fontWeight: FontWeight.w400,
          height: 1.60,
          letterSpacing: 0.28,
        ),
        decoration: InputDecoration(
          hintText: hintText,
          hintStyle: TextStyle(
            color: Colors.black.withValues(alpha: 0.39),
            fontSize: 14.sp,
            fontFamily: 'Manrope',
            fontWeight: FontWeight.w400,
            height: 1.60,
            letterSpacing: 0.28,
          ),
          prefixIcon: prefixIcon,
          suffixIcon: suffixIcon,
          border: InputBorder.none,
          contentPadding: EdgeInsets.symmetric(
            horizontal: 14.w,
            vertical: 10.h,
          ),
          counterText: maxLength != null ? '' : null, // 隐藏字符计数
        ),
      ),
    );
  }
}
